package com.zhongxin.wymovieticket;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhoujing.Entity.ActityHotEntity;
import com.zhoujing.adapter.ActivityListitemAdapter;
import com.zhoujing.utils.ImageDownload_NetworkImageView;
import com.zhoujing.utils.JsonUtils;
import com.zhoujing.utils.JsonUtils.JsonObjectCallBack;

public class SubPriceActivity extends Activity {

	private final static String PATH = "http://piao.163.com/m/promotion/list.html?type=2&city=440300&apiVer=14&mobileType=android&deviceId=08:00:27:3b:ad:5b&channel=netease&ver=4.8";
	private RequestQueue queue;
	private List<ActityHotEntity> list = new ArrayList<ActityHotEntity>();
	private PullToRefreshListView pullToRefreshListView;
	private ListView listView;
	private ActivityListitemAdapter activityListitemAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sub_price);
		queue = Volley.newRequestQueue(this);
		initView();
		
//		// 下载数据
      	GetData();

	}

	private void initView() {
		// TODO Auto-generated method stub
		pullToRefreshListView = (PullToRefreshListView) this
				.findViewById(R.id.activity_price_pulltolistview);
		listView = pullToRefreshListView.getRefreshableView();
		// 添加一个头
		TextView textView = new TextView(this);
		textView.setText("热门活动");
		listView.addHeaderView(textView);
		if (list!=null&&list.size()>0) {
			list.clear();
		}
		LruCache<String, Bitmap> lruCache=ImageDownload_NetworkImageView.getLruCache(this);
		activityListitemAdapter=new ActivityListitemAdapter(this,list, queue,lruCache );
		listView.setAdapter(activityListitemAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sub_price, menu);
		return true;
	}

	private void GetData() {
		JsonUtils.parseJsonObject(this, PATH, new JsonObjectCallBack() {

			@Override
			public void jsonObjectCallBack(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				try {
					List<ActityHotEntity> entities = new ArrayList<ActityHotEntity>();
					JSONArray array = jsonObject.getJSONArray("list");
					for (int i = 0; i < array.length(); i++) {
						JSONObject entity = array.getJSONObject(i);
						ActityHotEntity hotEntity = new ActityHotEntity();
						hotEntity.setTitle(entity.getString("title"));
						hotEntity.setPicUrl(entity.getString("picUrl"));
						hotEntity.setActivityStatus(entity
								.getInt("activityStatus"));
						hotEntity.setClickUrl(entity.getString("clickUrl"));
						hotEntity.setType(entity.getInt("type"));
						entities.add(hotEntity);
					}
					
					
           			list.addAll(entities);
					activityListitemAdapter.notifyDataSetChanged();

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}, queue);
	}

}
